<template>
  <div id="globalHeader">
    <a-row>
      <a-col flex="200px">
        <div class="title-bar">
          <img class="logo" src="../assets/logo.png" />
          <h2 class="title">编程聚落</h2>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="80px">
        <template v-if="loginUserStore.loginUser.id">
          <div>{{ loginUserStore.loginUser.userName ?? 无名 }}</div>
        </template>
        <template v-else>
          <a-button type="primary" href="/user/login" style="">登录</a-button>
        </template>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { MenuProps } from "ant-design-vue";
import { useRouter } from "vue-router";
import { routes } from "@/router/routes";
import { useLoginUserStore } from "@/score/user";

const current = ref<string[]>(["/"]);
const loginUserStore = useLoginUserStore();
const items = ref<MenuProps["items"]>([
  {
    key: "/",
    label: "主页",
    title: "主页",
  },
  {
    key: "/team",
    label: "队伍",
    title: "队伍",
  },
  {
    key: "/application",
    label: "应用",
    title: "应用",
  },
  {
    key: "/recommend",
    label: "推荐",
    title: "推荐",
  },
  {
    key: "/admin",
    label: "管理员",
    title: "管理员",
  },
]);
/**
 * 路由跳转
 */
const router = useRouter();
const doMenuClick = ({ key }: { key: string }) => {
  console.log(key);
  router.push({
    path: key,
  });
};
/**
 * 导航菜单高亮
 */
router.afterEach((to, from, failure) => {
  current.value = [to.path];
});
</script>

<style scoped>
#globalHeader {
}

.title-bar {
  /* flex布局，让div元素实现左右布局 */
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
